package com.delos.biz.manager;

import com.delos.api.manager.IRedisManager;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * Redis缓存测试
 *
 * @author zouyaowen
 * @date 2020-05-17
 */
@Service
@Slf4j
public class RedisManagerImpl implements IRedisManager {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public String getData(String key) {
        String value = (String) redisTemplate.opsForValue().get(key);
        log.info("valuezhi值为=[{}]", value);
        return value;
    }

    @Override
    public void setData(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void lock() {
        //redisTemplate获取锁实例
        ValueOperations<String, Object> valueOpt = redisTemplate.opsForValue();
        long currentTimeMillis = System.currentTimeMillis();
        String currentTimeStr = String.valueOf(currentTimeMillis);
        Boolean redisLock = false;
        try {
            redisLock = valueOpt.setIfAbsent("redis-lock", currentTimeStr, Duration.ofSeconds(20));
            if (redisLock != null && redisLock) {
                //业务执行
            }
        } catch (Exception e) {
            log.error("业务执行异常", e);
        } finally {
            if (redisLock != null && redisLock) {
                String lockKey = (String) valueOpt.get("redis-lock");
                if (currentTimeStr.equals(lockKey)) {
                    redisTemplate.delete("redis-lock");
                }
            }
        }

        //单锁获取
        RLock lock = redissonClient.getLock("aaa");
        try {
            boolean getLock = lock.tryLock(1, TimeUnit.SECONDS);
            if (getLock) {
                //业务执行
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
        //多锁获取，此处可以传多个锁对象,注意单锁与多锁释放方式不一致
        RLock multiLock = redissonClient.getMultiLock(lock);
        try {
            boolean getMultiLock = multiLock.tryLock(1, TimeUnit.SECONDS);
            if (getMultiLock) {
                //业务执行
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            multiLock.unlock();
        }
        //数据类型操作
        RList<Object> list = redissonClient.getList("list");
        list.add("aaa");
        RMap<Object, Object> bbb = redissonClient.getMap("bbb");
        bbb.put("bbb", "bbb");
        RSet<Object> ccc = redissonClient.getSet("ccc");
        ccc.add("ccc");
        RSortedSet<Object> ddd = redissonClient.getSortedSet("ddd");
        ddd.add("ddd");
    }
}
